﻿using System.Collections.Generic;
using System.Data;
using System.Text;
using ProjectCommon2.Helpers;

namespace PolePosition.v3.EntityFactory
{
    public sealed partial class UtilityAssetDistributionFactory
    {
        public static int[] ValidateDistributionAssetId(int utilityId, int[] ids)
        {
            //Prepare Ids for procedure
            var sbIds = new StringBuilder();
            foreach (var id in ids)
                sbIds.Append(id + ",");

            // build command
            var SqlCmd = new System.Data.SqlClient.SqlCommand();
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.CommandText = "up_Utility_Asset_Distribution_ValidateIds";

            SqlCmd.Parameters.AddWithValue("@utilityId", utilityId);
            SqlCmd.Parameters.AddWithValue("@assetIdList", sbIds.ToString());

            var notValidIdsDataTable = SqlHelper.ExecuteDataTable(SqlCmd);
            var notValidIds = new List<int>();

            foreach (DataRow row in notValidIdsDataTable.Rows)
                notValidIds.Add(row.Field<int>("AssetId"));

            return notValidIds.ToArray();
        }

        public static int[] ValidateMeterAssetId(int utilityId, int[] ids)
        {
            //Prepare Ids for procedure
            var sbIds = new StringBuilder();
            foreach (var id in ids)
                sbIds.Append(id + ",");

            // build command
            var SqlCmd = new System.Data.SqlClient.SqlCommand();
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.CommandText = "up_Utility_Asset_Distribution_ValidateMeterIds";

            SqlCmd.Parameters.AddWithValue("@utilityId", utilityId);
            SqlCmd.Parameters.AddWithValue("@assetIdList", sbIds.ToString());

            var notValidIdsDataTable = SqlHelper.ExecuteDataTable(SqlCmd);
            var notValidIds = new List<int>();

            foreach (DataRow row in notValidIdsDataTable.Rows)
                notValidIds.Add(row.Field<int>("AssetId"));

            return notValidIds.ToArray();
        }

        public static int[] ValidateInspectionAssetId(int utilityId, int[] ids)
        {
            //Prepare Ids for procedure
            var sbIds = new StringBuilder();
            foreach (var id in ids)
                sbIds.Append(id + ",");

            // build command
            var SqlCmd = new System.Data.SqlClient.SqlCommand();
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.CommandText = "up_Utility_Asset_XMissionStructures_Poles_ValidateIds";

            SqlCmd.Parameters.AddWithValue("@utilityId", utilityId);
            SqlCmd.Parameters.AddWithValue("@assetIdList", sbIds.ToString());

            var notValidIdsDataTable = SqlHelper.ExecuteDataTable(SqlCmd);
            var notValidIds = new List<int>();

            foreach (DataRow row in notValidIdsDataTable.Rows)
                notValidIds.Add(row.Field<int>("AssetId"));

            return notValidIds.ToArray();
        }

        public static void UpdateParentsByXml(string xml)
        {
            // build command
            var SqlCmd = new System.Data.SqlClient.SqlCommand();
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.CommandText = "up_UtilityAssetDistribution_UpdateParents";

            //add param Vendor_ProjectFK

            SqlCmd.Parameters.AddWithValue("@parentsIds", xml);

            SqlHelper.ExecuteNonQuery(SqlCmd);
        }
    }
}
